package com.ea.config;

import com.ea.common.constant.RedisKeyConstants;
import com.ea.service.IOrderInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

/**
 * @author NAME-L
 * @Description 用于监听过期的待支付订单信息数据
 * @className RedisListenerConfig
 * @date 2023-01-15 01:29:36
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
    private static final Logger logger = LoggerFactory.getLogger(RedisKeyExpirationListener.class);

    @Autowired
    private IOrderInfoService orderInfoService;
    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String key = message.toString();
        // 过期未支付订单处理
        if (key != null && key.startsWith(RedisKeyConstants.BE_PAY_ORDER)) {
            Long orderId = Long.valueOf(key.substring(RedisKeyConstants.BE_PAY_ORDER.length()));
            logger.info("time out be pay order ====> orderId:{}" , orderId);
            orderInfoService.cancelOrderFromNoPay(orderId);
        }
    }
}
